package com.example.management.service.impl;

import com.example.management.mapper.BuildingMapper;
import com.example.management.pojo.Building;
import com.example.management.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {
    @Autowired
    private BuildingMapper buildingMapper;

    @Override
    public List<Building> getAllBuilding() {
        return buildingMapper.getAllBuilding();
    }

    @Override
    public String changeBuilding(Building building) {
        try {
            if (buildingMapper.selectBuildingByNum(building.getNumber()) != null) {
                buildingMapper.updateBuilding(building);
                return "修改成功！";
            } else {
                return "没有这个楼栋。";
            }

        } catch (Exception e) {
            return "Error!";
        }
    }

    @Override
    public String deleteBuilding(Integer number) {
        try {
            if (buildingMapper.selectBuildingByNum(number) != null) {
                buildingMapper.deleteBuilding(number);
                return "删除学号为" + number + "的楼栋成功";
            } else {
                return "查无此楼";
            }
        } catch (Exception e) {
            return "Error";
        }
    }

    @Override
    public String addBuilding(Building building) {
        try {
            if (buildingMapper.selectBuildingByNum(building.getNumber()) == null) {
                buildingMapper.insertBuilding(building);
                return "添加成功" + "楼栋号码,牌号,地址分别是：" + building.getNumber() + "," + building.getTag() + "," + building.getPosition();
            } else {
                return "查无此楼";
            }
        } catch (Exception e) {
            return "Error";
        }
    }
}
